//index.js
//获取应用实例
const app = getApp();
Page({
  data: { 
    active:0,
    cates:[],
    loading: 'false',
    haveMore: 'true',
    triggered: false
  },
   
  onLoad: function () {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          deviceHeight: ((res.windowHeight * 750) / res.windowWidth)
        })
      }
    });
    this.getCate(() => {
      this.selectComponent('.scroll0').refresh();
    });
  },
 
  changetab(e) {
    let { index } = e.detail;
    this.setData({
      active: index
    });
    if (this.data.cates[index].list.length <= 0) {
      let sclass = '.scroll' + index;
      this.selectComponent(sclass).startDown();
    }
  
  },
  getCate(fn = null) {
    let that = this;
    app.http.$ajax({
      url: 'v1.video/getCate',
      loading: false
    }, 
    {
      success(res) {
        //这是个3维数组
        let cates = [...res.data];
        cates.forEach((item) => {
          item.post = {
            cate_id: item.id,
          }
          item.list = [];
        }) ;
        
        //把第一个设置为默认选择
        that.setData({
          cates: cates,
        });
        app.util.isFunction(fn) && fn();
      }
    });
  },

 
  refresh(res) {
    let {index} = res.target.dataset;
    let data = res.detail.data.data;
    this.data.cates[index].list = data;
    this.setData({
      cates: this.data.cates
    });
  },
  more(res) {
    let { index } = res.target.dataset;
    let data = res.detail.data.data;
    this.data.cates[index].list.push(...data);
    this.setData({
      cates: this.data.cates
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let index = this.data.active;
    let sclass = '.scroll' + index;
    this.selectComponent(sclass).more('more');
  },
})
